library CriticalStrike initializer init requires LegacySystem


	private function CriticalStrikeLearnConditions takes nothing returns boolean
		local integer i =  GetLearnedSkill()
		return i == 'A04M' or i == 'A040' or i == 'A08E'
	endfunction
	
	private function CriticalStrikeLearn takes nothing returns nothing
		local unit u = GetTriggerUnit()
		local player p = GetOwningPlayer(u)
		local integer i
		
		if GetLearnedSkill()=='A04M' then
			if GetPlayerTechCount(p,'R00M',true)==0 then
				call SetPlayerTechResearched(p,'R00M',1)
			else
				set i = 'A04L' //Regular Crit
				call IncUnitAbilityLevel(u,i)
			endif
		elseif GetLearnedSkill()=='A040' then
			if GetPlayerTechCount(p,'R016',true)==0 then
				call SetPlayerTechResearched(p,'R016',1)
			else
				set i = 'A04V' //Marksman Crit
				call IncUnitAbilityLevel(u,i)
			endif
		elseif GetLearnedSkill()=='A08E' then
			if GetPlayerTechCount(p,'R00Y',true)==0 then
				call SetPlayerTechResearched(p,'R00Y',1)
			endif
			set i = 'A078' //Combat Engineer Actual Crit
			call IncUnitAbilityLevel(u,i)
			set udg_ReloadTime[GetConvertedPlayerId(p)] = udg_ReloadTime[GetConvertedPlayerId(p)] - 0.5
		endif
			
		
		set u = null
	endfunction

	private function init takes nothing returns nothing
		local trigger t
		set t = CreateTrigger()
		call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_HERO_SKILL)
		call TriggerAddCondition(t, Condition(function CriticalStrikeLearnConditions))
		call TriggerAddAction(t,function CriticalStrikeLearn)
	endfunction

endlibrary